<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<script type="text/javascript">
	function loadStaffRole1(){
		//因为tab初始化的时候是没有宽度的，延后200毫秒执行才可以得到grid的宽度
		setTimeout("loadStaffRole()",200)
	}
	var role_grid_create = false;
	function loadStaffRole(){
		var params = {staff_id:$("#staff_edit").attr("staff_id")};
		if(role_grid_create){
			$("#role_grid").setGridParam({postData:params}).trigger("reloadGrid"); 
		}
		role_grid_create = true;
		$("#role_grid").jqGridInit({
			postData:params,
			url : "/admin/staff/loadStaffRolePage.do",
			colNames : [ '角色ID','ID', '角色名称','具体描述' ],
	        colModel : [ 
						{name : 'role_id',index : 'role_id',hidden:true,}, 
						{name : 'rel_id',index : 'rel_id',hidden:true,key:true,}, 
						{name : 'role_name',index : 'role_name',editable: true,editrules:{required:true,}}, 
						{name : 'role_desc',index : 'role_desc',editable: true,editrules:{required:true,}}, 
	                   ],
	       	autowidth:true,
	        height:250,
	        multiselect:false,
		});
	}
	function addStaffRole(){
		var html = $("#staffSelectWindow").html();
		bootbox.dialog({
			message	: html,
			title:"选择组织",
			buttons	:{
				"success" :
				 {
					"label" : "<i class='ace-icon fa fa-check'></i>提交",
					"className" : "btn-sm btn-success",
					"callback": function() {
						var rowData = document.getElementsByName("iframe_staffRole_select")[1].contentWindow.returnSelectRow();
						if(!rowData.role_id){
							return ;
						}
						var data = {};
						data.staff_id = $("#staff_edit").attr("staff_id");
						data.role_id = rowData.role_id;
						
						$.ajax({
							url : "/admin/staff/addStaffRoleRel.do",
							type:"post",
							dataType:"json",
							data:data,
							success : function(data) {
								$("#role_grid").trigger("reloadGrid");
							}
						});
					}
				},
				"button" :
				{
					"label" : "<i class='ace-icon fa fa-times'></i>取消",
					"className" : "btn-sm",
					"callback": function() {
						//if(clear)clear();
					}
				}
			}
		});
	}
	function deleteStaffRole(){
		var id = $("#role_grid").jqGrid("getGridParam","selrow");
		
		var param = {};
		param.rel_id = id;
		var callBack = function(){
			$.ajax( {
				url : "/admin/staff/delStaffRoleRel.do",
				type:"post",
				dataType:"json",
				data:param,
				success : function(data) {
					$.gritter.add({
						title: '提示',
						text: data.msg,
						class_name: 'gritter-success'
					});
					$("#role_grid").trigger("reloadGrid");
				}
			});
		};
		bootbox.confirm("是否删除用户的角色?", function(result) {
			if(result) {
				callBack();
			}
		});
	}
</script>
<!-- 用户-->
<div>
	<table id="role_grid"></table>
</div>
<div style="margin-top: 10px;">
	<button class="btn btn-primary btn-sm" onclick="addStaffRole()">
		<i class="ace-icon fa fa-plus-circle bigger-120"></i>
		添加角色
	</button>
	<button class="btn btn-warning btn-sm" onclick="deleteStaffRole()">
		<i class="ace-icon fa fa-trash-o bigger-120 orange"></i>
		删除角色
	</button>
</div>

<div id="staffSelectWindow" style="display: none;">
	<iframe src="/admin/common/role_select.jsp?frame=1"
		 style="width: 100%;height: 374px;border: 0px;" name="iframe_staffRole_select"></iframe>
</div>